const mongoose = require("mongoose");

const itemSchema = new mongoose.Schema(
  {
    itemCode: { 
      type: String, 
      required: true, 
      unique: true 
    },
    itemName: { 
      type: String, 
      required: true 
    },
    brand: { 
      type: mongoose.Schema.Types.ObjectId, 
      ref: "Brand",
    },

    subCategory: { type: mongoose.Schema.Types.ObjectId, ref: "SubCategory" },

    
    category: { 
      type: mongoose.Schema.Types.ObjectId, 
      ref: "Category", 
      required: true 
    },
    itemGroup: { 
      type: String, 
      required: true, 
      default: "Single" 
    },
    unit: { type: mongoose.Schema.Types.ObjectId, ref: "Unit", required: true },
    sku: { type: String },
    hsn: { type: String },
    barcode: { type: String },
    alertQuantity: { 
      type: Number, 
      default: 0 
    },
    description: { type: String },
    discountType: { 
      type: String, 
      enum: ["Percentage", "Fixed"], 
      default: "Percentage" 
    },
    discount: { 
      type: Number, 
      default: 0 
    },
    priceWithoutTax: { 
      type: Number, 
      required: true 
    },
    purchasePrice: { 
      type: Number, 
      required: true 
    },
    salesPrice: { 
      type: Number, 
      required: true 
    },
    tax: { type: mongoose.Schema.Types.ObjectId, ref: "Tax" },
    profitMargin: { type: Number },
    mrp: { type: Number },
    expiryDate: { type: Date },
    
    warehouse: { 
      type: mongoose.Schema.Types.ObjectId, 
      ref: "Warehouse", 
      required: true 
    },
    openingStock: { 
      type: Number, 
      default: 0 
    },
    
    itemImage: { 
      type: String 
    },
  },
  { timestamps: true }
);

module.exports = mongoose.model("Item", itemSchema);
